define(function (require) {


    require('./logistic-tpl');

    var Tpl = juicer.templates['logistic.juicer.html'];


    var logistic = {
        dialog:{},
        ids: [],
        /**
         * 初始化
         * @param _ids
         * @param _logisticData
         */
        init: function (_ids, _logisticData) {
            var self = this;
            self.ids = _ids;
            self.dialog = window.dialog({
                title: '物流信息修改',
                quickClose: false,
                width: 800,
                zIndex: 1109,
                fixed: false,
                content: Tpl({"data": _logisticData}),
                cancelValue: '关闭',
                cancel: function () {
                },
                okValue: '保存',
                ok: function () {
                    self.updateBuyerInfo();
                    return false;
                }
            });
            self.dialog.showModal();
            self.initDivision(_logisticData);
            self.initValidator();
        },
        /**
         * 获取区域信息
         * @param _logisticData
         */
        initDivision: function (_logisticData) {
            var self = this;
            CRM.request({
                "type": 'GET',
                "url": CRM.getOuterUrl("/api/rest/division.do"),
                "dataType": "json",
                contentType: "application/x-www-form-urlencoded",
                "data": {
                    "code": _logisticData.buyerCityCode
                },
                "success": function (req) {
                    if (req.code && req.code == 10000) {
                        $("#districtName").html("");
                        $.each(req.data.division.subDivisions, function (index, item) {
                            $("#districtName").append("<option value='" + item.name + "'>" + item.name + "</option>");
                        });
                        if (_logisticData.districtName) {
                            $("#districtName").val(_logisticData.districtName);
                        }
                    } else {
                        CRM.showDialog(req.msg);
                    }
                }
            });
        },
        initValidator: function () {
            $('#logistic_form').bootstrapValidator({
                message: '输入错误',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    mobile: {
                        message: '手机号码输入错误',
                        validators: {
                            notEmpty: {
                                message: '手机号码不能为空'
                            },
                            phone: {
                                country: 'CN',
                                message: '请输入正确的电话格式'
                            }
                        }
                    },
                    reason: {
                        message: '发起原因输入错误',
                        validators: {
                            notEmpty: {
                                message: '发起原因不能为空'
                            }
                        }
                    }
                }
            });
        },
        updateBuyerInfo:function(){
            $("#logistic_form").data('bootstrapValidator').resetForm();
            $("#logistic_form").data('bootstrapValidator').validate();
            var dataString, _data = {};
            $($("#logistic_form").serializeArray()).each(function () {
                _data[this.name] = this.value;
            });
            var self = this;
            _data.stockOutIds = self.ids;
            if ($("#logistic_form").data('bootstrapValidator').isValid()) {
                CRM.request({
                    "type": 'POST',
                    "url": CRM.getOuterUrl("/logisticscentral/crm/stockout/updateBuyerInfo"),
                    "dataType": "json",
                    "data": JSON.stringify(_data),
                    "success": function (req) {
                        if (req.status) {
                            CRM.showDialog("修改成功！");
                            self.dialog.remove();
                            $("#order-detail-modal").modal("hide");
                        } else {
                            CRM.showDialog(req.message);
                        }
                    }
                });
            }
        }

    };

    return logistic

});